package com.city.dispatch.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.city.dispatch.R;
import com.city.dispatch.activity.LoginActivity;
import com.city.dispatch.base.BaseFragment;
import com.city.dispatch.core.common.HttpHelper;
import com.city.dispatch.core.common.SharedPreferencesUrls;
import com.city.dispatch.core.common.UIHelper;
import com.city.dispatch.core.common.Urls;
import com.city.dispatch.core.widget.LoadingDialog;
import com.city.dispatch.model.ResultConsel;
import com.city.dispatch.utils.LogUtil;
import com.city.dispatch.utils.ToastUtil;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/10/18 0018.
 */

public class DispatchStartEndFragment extends BaseFragment implements View.OnClickListener {
    Unbinder unbinder;
    private View v;

//    @BindView(R.id.mainUI_title_backBtn)
//    ImageView backBtn;
//    @BindView(R.id.mainUI_title_rightBtn)
//    TextView rightBtn;
//    @BindView(R.id.mainUI_title_titleText)
//    TextView titleText;
    @BindView(R.id.ui_merchantAddress_map)
    MapView mapView;
//    @BindView(R.id.et_name)
//    EditText codeNumEdit;
//    @BindView(R.id.searchBtn)
//    TextView searchBtn;
    @BindView(R.id.tv_day)
    TextView tv_day;
//    @BindView(R.id.ll_car_search)
//    LinearLayout ll_car_search;

    private Context context;
    private Activity activity;
//    private LoadingDialog loadingDialog;

    private double latitude;
    private double longitude;
    private AMap aMap;
    private static final int STROKE_COLOR = Color.argb(80, 3, 0, 255);
    private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);
    private LatLng myLocation = null;
    private Marker centerMarker;
    private BitmapDescriptor successDescripter;
    private BitmapDescriptor bikeDescripter;
    private BitmapDescriptor startDescripter;
    private BitmapDescriptor endDescripter;

    private Polyline polyline;
    private PolylineOptions mPolyoptions;

    private Marker originMarker;
    private Marker terminusMarker;

    private String begintime;
    private String endtime;

    private String type;

    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

    private int carmodel_id;
    private String codenum;

    double start_longitude;
    double start_latitude;
    double end_longitude;
    double end_latitude;

//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.ui_merchant_address_map);
//        ButterKnife.bind(this);
//
//        init();
//    }

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_dispatch_start_end, null);
        unbinder = ButterKnife.bind(this, v);

        return v;
    }


    @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
        activity = getActivity();

        mapView.onCreate(savedInstanceState);// 此方法必须重写

        if(activity.getIntent().getStringExtra("start_longitude")!=null && !"".equals(activity.getIntent().getStringExtra("start_longitude"))){
            start_longitude = Double.parseDouble(activity.getIntent().getStringExtra("start_longitude"));
        }

        if(activity.getIntent().getStringExtra("start_latitude")!=null && !"".equals(activity.getIntent().getStringExtra("start_latitude"))){
            start_latitude = Double.parseDouble(activity.getIntent().getStringExtra("start_latitude"));
        }

        if(activity.getIntent().getStringExtra("end_longitude")!=null && !"".equals(activity.getIntent().getStringExtra("end_longitude"))){
            end_longitude = Double.parseDouble(activity.getIntent().getStringExtra("end_longitude"));
        }

        if(activity.getIntent().getStringExtra("end_latitude")!=null && !"".equals(activity.getIntent().getStringExtra("end_latitude"))){
            end_latitude = Double.parseDouble(activity.getIntent().getStringExtra("end_latitude"));
        }





//        initHttp();

        init();
    }

    private void init(){

        Date date=new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
//        date=calendar.getTime(); //这个时间就是日期往后推一天的结果

        begintime = formatter.format(date);
        endtime = formatter.format(calendar.getTime());

//        tv_day.setText("日期范围："+begintime+" 到 "+endtime);

//        titleText.setText("车辆位置");
//        rightBtn.setText("选择日期");
        loadingDialog = new LoadingDialog(context);
        loadingDialog.setCancelable(false);
        loadingDialog.setCanceledOnTouchOutside(false);
        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }
        aMap.getUiSettings().setZoomControlsEnabled(false);
        aMap.getUiSettings().setMyLocationButtonEnabled(false);

        successDescripter = BitmapDescriptorFactory.fromResource(R.drawable.icon_usecarnow_position_succeed);
        bikeDescripter = BitmapDescriptorFactory.fromResource(R.drawable.bike_icon);
        startDescripter = BitmapDescriptorFactory.fromResource(R.drawable.start_pin_icon);
        endDescripter = BitmapDescriptorFactory.fromResource(R.drawable.end_pin_icon);

//        CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(16);// 设置缩放监听
//        aMap.moveCamera(cameraUpdate);


        myLocation = new LatLng(ScanFragment.latitude, ScanFragment.longitude);
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 16));



        LogUtil.e("dsef===init",start_latitude+"==="+start_longitude+"==="+end_latitude+"==="+end_longitude);

//        myLocation = new LatLng(start_latitude, start_longitude);
//        addChooseMarker(true, new LatLng(start_latitude, start_longitude));
//        addChooseMarker(false, new LatLng(end_latitude, end_longitude));

//        MarkerOptions centerMarkerOption = new MarkerOptions().position(new LatLng(start_latitude, start_longitude)).icon(startDescripter);

        LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();//存放所有点的经纬度

        if(start_latitude!=0d && start_longitude!=0d){
            LatLng startLatLng = new LatLng(start_latitude, start_longitude);
            aMap.addMarker(new MarkerOptions().position(startLatLng).icon(startDescripter));
            boundsBuilder.include(startLatLng);
        }

        if(end_latitude!=0d && end_longitude!=0d){
            LatLng endLatLng = new LatLng(end_latitude, end_longitude);
            aMap.addMarker(new MarkerOptions().position(endLatLng).icon(endDescripter));
            boundsBuilder.include(endLatLng);
        }


//        230 * Math.round(BaseApplication.density)
        aMap.moveCamera(CameraUpdateFactory.newLatLngBoundsRect(boundsBuilder.build(),100,100,100,100));    //left_right_padding, left_right_padding, top_padding, bottom_padding


//        codeNumEdit.setText(codenum);
//
//        if(carmodel_id==2){
//            ll_car_search.setVisibility(View.VISIBLE);
//        }else{
//            ll_car_search.setVisibility(View.GONE);
//        }
//
//        backBtn.setOnClickListener(this);
//        searchBtn.setOnClickListener(this);
//        ll_car_search.setOnClickListener(this);


//        myLocation = new LatLng(latitude,longitude);
//        addChooseMarker();
//        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 12));

    }

    private void initHttp() {
//        String uid = SharedPreferencesUrls.getInstance().getString("uid","");
        String access_token = SharedPreferencesUrls.getInstance().getString("access_token","");
        if (access_token == null || "".equals(access_token)){
            Toast.makeText(context,"请先登录您的账号",Toast.LENGTH_SHORT).show();
            UIHelper.goToAct(context,LoginActivity.class);
            return;
        }
//        RequestParams params = new RequestParams();
//        params.put("codenum",codenum);
        HttpHelper.get(context, Urls.car+codenum, new TextHttpResponseHandler() {
            @Override
            public void onStart() {
                onStartCommon("正在加载");
            }
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                onFailureCommon(throwable.toString());
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, final String responseString) {
                m_myHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        LogUtil.e("mama===car","==="+responseString);
                        try {
                            ResultConsel result = JSON.parseObject(responseString, ResultConsel.class);

                            if(result.getStatus_code()==0){
                                JSONObject jsonObject = new JSONObject(result.getData());

//                                if("".equals(jsonObject.getString("latitude"))){
//                                    myLocation = new LatLng(31.76446, 119.920594);
//                                }else{
//                                    myLocation = new LatLng(Double.parseDouble(jsonObject.getString("latitude")), Double.parseDouble(jsonObject.getString("longitude")));
//                                }
//                                addChooseMarker();
//                                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 16));
//
////                                codenum = jsonObject.getString("number");
//                                carmodel_id = jsonObject.getInt("carmodel_id");


                            }else{

                                ToastUtil.showMessageApp(context, result.getMessage());
                                if(centerMarker!=null){
                                    centerMarker.remove();
                                    centerMarker = null;
                                }

                            }


//                            if (result.getFlag().equals("Success")){
//                                JSONObject jsonObject = new JSONObject(result.getData());
//                                if (!"[]".equals(result.getData())){
//
//                                    type = jsonObject.getString("type");
//
//                                    if("4".equals(type) || "7".equals(type)){
//                                        gpstrack(codenum);
//                                    }else{
//                                        tv_day.setText("");
//
//                                        myLocation = new LatLng(Double.parseDouble(jsonObject.getString("latitude")), Double.parseDouble(jsonObject.getString("longitude")));
//                                        addChooseMarker();
//                                        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 16));
//                                    }
//
//                                }
//                            } else {
//                                Toast.makeText(context,result.getMsg(),Toast.LENGTH_SHORT).show();
//                            }
                        } catch (Exception e) {
                            LogUtil.e("Test","异常:"+e);
                        }
                        if (loadingDialog != null && loadingDialog.isShowing()){
                            loadingDialog.dismiss();
                        }
                    }
                });

            }
        });
    }

    private void gpstrack(String codenum){

        final String uid = SharedPreferencesUrls.getInstance().getString("uid","");
        final String access_token = SharedPreferencesUrls.getInstance().getString("access_token","");
        if (uid == null || "".equals(uid) || access_token == null || "".equals(access_token)){
            ToastUtil.showMessageApp(context,"请先登录账号");
            UIHelper.goToAct(context, LoginActivity.class);
        }else {

//            if (begintime == null || "".equals(begintime)){
//                Toast.makeText(context,"请选择日期",Toast.LENGTH_SHORT).show();
//                return;
//            }
//
//            if (endtime == null || "".equals(endtime)){
//                Toast.makeText(context,"请选择日期",Toast.LENGTH_SHORT).show();
//                return;
//            }
//
//            tv_day.setText("日期范围："+begintime+" 到 "+endtime);

            if (polyline != null) {
                polyline.remove();
                polyline = null;
            }

            if(originMarker!=null){
                originMarker.remove();
            }

            if(terminusMarker!=null){
                terminusMarker.remove();
            }

            mPolyoptions = new PolylineOptions();
            mPolyoptions.width(40f);
            mPolyoptions.setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.grasp_trace_line));

            RequestParams params = new RequestParams();
            params.put("uid", uid);
            params.put("access_token", access_token);
            params.put("tokencode", codenum);        //80001819、80002041
            params.put("begintime", begintime);
            params.put("endtime", endtime);
            HttpHelper.get(context, Urls.gpstrack, params, new TextHttpResponseHandler() {
                @Override
                public void onStart() {
                    onStartCommon("正在加载");
                }
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    onFailureCommon(throwable.toString());
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, final String responseString) {
                    m_myHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                ResultConsel result = JSON.parseObject(responseString, ResultConsel.class);
                                if (result.getFlag().equals("Success")) {
                                    JSONArray array = new JSONArray(result.getData());

//                                    LogUtil.e("gpstrack===","==="+responseString);
//
//                                    if (0 == array.length()){
//                                        ToastUtil.showMessageApp(context, "没有轨迹");
//                                    }else {
//                                        for (int i = 0; i < array.length(); i++){
//                                            GPSTrackBean bean = JSON.parseObject(array.getJSONObject(i).toString(), GPSTrackBean.class);
//                                            // 加入自定义标签
//
//                                            LatLng latLng = new LatLng(Double.parseDouble(bean.getLat()),Double.parseDouble(bean.getLon()));
//
//                                            if(i==0){
//                                                MarkerOptions bikeMarkerOption = new MarkerOptions().position(latLng).icon(originDescripter);
//                                                originMarker = aMap.addMarker(bikeMarkerOption);
//
//                                                aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
//                                            }else if(i==array.length()-1){
//                                                MarkerOptions bikeMarkerOption = new MarkerOptions().position(latLng).icon(terminusDescripter);
//                                                terminusMarker = aMap.addMarker(bikeMarkerOption);
//                                            }else{
//                                            }
//
//
//                                            mPolyoptions.add(latLng);
//
//                                            LogUtil.e("gpstrack===",polyline+"==="+mPolyoptions.getPoints().size());
//
//                                            if (mPolyoptions.getPoints().size() > 1) {
//                                                if (polyline != null) {
//                                                    polyline.setPoints(mPolyoptions.getPoints());
//                                                } else {
//                                                    polyline = aMap.addPolyline(mPolyoptions);
//                                                }
////                                        polyline = aMap.addPolyline(mPolyoptions);
//                                            }
//
////                                    bikeMarkerList.add(bikeMarker);
//                                        }
//                                    }
                                } else {
                                    ToastUtil.showMessageApp(context,result.getMsg());
                                }
                            } catch (Exception e) {

                            }
                            if (loadingDialog != null && loadingDialog.isShowing()){
                                loadingDialog.dismiss();
                            }
                        }
                    });

                }
            });
        }

    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;
                default:
                    break;
            }
            return false;
        }
    });


    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            case R.id.mainUI_title_backBtn:
//                scrollToFinishActivity();
//                break;
//            case R.id.mainUI_title_rightBtn:
//                Intent intent = new Intent(context, HistoryRoadFiltateActivity.class);
//                startActivityForResult(intent,0);
//                break;
//            case R.id.searchBtn:
//                codenum = codeNumEdit.getText().toString().trim();
//                if (codenum == null || "".equals(codenum)){
//                    Toast.makeText(context,"请输入车辆编号",Toast.LENGTH_SHORT).show();
//                    return;
//                }
//
//                initHttp();
//                break;

            case R.id.ll_car_search:
                ddSearch();
                break;

            default:
                break;
        }
    }

    private void ddSearch(){
        LogUtil.e("ddSearch===", "==="+codenum);

        String access_token = SharedPreferencesUrls.getInstance().getString("access_token","");
        if (access_token == null || "".equals(access_token)){
            Toast.makeText(context,"请先登录账号",Toast.LENGTH_SHORT).show();
            UIHelper.goToAct(context, LoginActivity.class);
        }else {
//            RequestParams params = new RequestParams();
//            params.put("tokencode",codenum);
            HttpHelper.post(context, Urls.search+codenum, null, new TextHttpResponseHandler() {
                @Override
                public void onStart() {
                    if (loadingDialog != null && !loadingDialog.isShowing()) {
                        loadingDialog.setTitle("正在提交");
                        loadingDialog.show();
                    }
                }
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    if (loadingDialog != null && loadingDialog.isShowing()){
                        loadingDialog.dismiss();
                    }
                    UIHelper.ToastError(context, throwable.toString());
                }
                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                    try {
                        LogUtil.e("ddSearch===1", "==="+responseString);

                        ResultConsel result = JSON.parseObject(responseString, ResultConsel.class);

//                        if(result.getStatus_code()==200){
//                            curMarker.setIcon(bikeDescripter_blue);
//                        }else{
//
//                        }

                        Toast.makeText(context, result.getMessage(), Toast.LENGTH_SHORT).show();

//                        if (result.getFlag().equals("Success")) {
//                            Toast.makeText(context,"发送寻车指令成功",Toast.LENGTH_SHORT).show();
//                        }else {
//                            Toast.makeText(context,result.getMsg(),Toast.LENGTH_SHORT).show();
//                        }
                    } catch (Exception e) {
                    }
                    if (loadingDialog != null && loadingDialog.isShowing()){
                        loadingDialog.dismiss();
                    }
                }
            });
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        switch (requestCode) {
//            case 0:
//                if (data != null) {
//                    begintime = data.getExtras().getString("starttime");
//                    endtime = data.getExtras().getString("endtime");
//
//                    tv_day.setText("日期范围："+begintime+" 到 "+endtime);
//
//                    LogUtil.e("onActivityResult===",begintime+"==="+endtime);
//
//                    codenum = codeNumEdit.getText().toString().trim();
//                    if (codenum == null || "".equals(codenum)){
//                        Toast.makeText(context,"请输入车辆编号",Toast.LENGTH_SHORT).show();
//                        return;
//                    }
//
//                    initHttp();
//                }
//                break;
//
//            default:
//                break;
//        }
//    }

    /**
     * 设置一些amap的属性
     */
    private void setUpMap() {
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(false);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        setupLocationStyle();
    }

    /**
     * 设置自定义定位蓝点
     */
    private void setupLocationStyle() {
        // 自定义系统定位蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 自定义定位蓝点图标
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
                fromResource(R.drawable.bike_icon));
        // 自定义精度范围的圆形边框颜色
        myLocationStyle.strokeColor(STROKE_COLOR);
        //自定义精度范围的圆形边框宽度
        myLocationStyle.strokeWidth(5);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(FILL_COLOR);
        // 将自定义的 myLocationStyle 对象添加到地图上
        aMap.setMyLocationStyle(myLocationStyle);
    }

//    /**
//     * 方法必须重写
//     */
//    @Override
//    protected void onResume() {
//        super.onResume();
//        mapView.onResume();
//    }
//
//    /**
//     * 方法必须重写
//     */
//    @Override
//    protected void onPause() {
//        super.onPause();
//        mapView.onPause();
//    }
//
//    /**
//     * 方法必须重写
//     */
//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
//        mapView.onSaveInstanceState(outState);
//    }
//
//    /**
//     * 方法必须重写
//     */
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        mapView.onDestroy();
//    }

    private void addChooseMarker(boolean isStart, LatLng location) {
        // 加入自定义标签

        MarkerOptions centerMarkerOption = new MarkerOptions().position(location).icon(successDescripter);
        aMap.addMarker(centerMarkerOption);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                CameraUpdate update = CameraUpdateFactory.zoomTo(16);
//                aMap.animateCamera(update, 1000, new AMap.CancelableCallback() {
//                    @Override
//                    public void onFinish() {
//                    }
//
//                    @Override
//                    public void onCancel() {
//
//                    }
//                });
//            }
//        }, 1000);

//        if(centerMarker==null){
//
//        }else{
//            centerMarker.setPosition(myLocation);
//        }
    }

    private void addChooseMarker(LatLng location) {
        // 加入自定义标签

        if(centerMarker==null){
            MarkerOptions centerMarkerOption = new MarkerOptions().position(location).icon(successDescripter);
            centerMarker = aMap.addMarker(centerMarkerOption);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    CameraUpdate update = CameraUpdateFactory.zoomTo(16);
                    aMap.animateCamera(update, 1000, new AMap.CancelableCallback() {
                        @Override
                        public void onFinish() {
                        }

                        @Override
                        public void onCancel() {

                        }
                    });
                }
            }, 1000);
        }else{
            centerMarker.setPosition(myLocation);
        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            scrollToFinishActivity();
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
}
